package com.jetpackmvp.widgets;

import android.app.AlertDialog;
import android.content.Context;
import android.view.View;
import android.widget.TextView;
import com.jetpackmvp.R;

/**
 * 自定义view--加载dialog
 */
public class ProcessDialog
        extends AlertDialog.Builder {
    private String _title = "请稍候";
    private AlertDialog alertDialog;
    private Context context;

    public ProcessDialog(Context context) {
        super(context, R.style.ProcessDialog);
        this.context = context;
    }

    /**
     * 取消dialog
     */
    public void dismiss() {
        if (alertDialog != null && alertDialog.isShowing()) {
            try {
                alertDialog.dismiss();
            }catch (Exception ignored){

            }
        }
    }

    /**
     * 展示dialog
     * @param cancel
     */
    private void showDialog(boolean cancel) {
        alertDialog = super.show();
        View view = View.inflate(context,R.layout.layout_progress_dialog, null);
        TextView tvTitle = (TextView) view.findViewById(R.id.tv_process_title);
        alertDialog.getWindow().setContentView(view);
        alertDialog.setCancelable(cancel);
        tvTitle.setText(_title + "...");
    }

    public void showWithCancel(){
        showDialog(true);
    }

    public void showNormal(){
        showDialog(false);
    }

    public ProcessDialog setTitle(String text) {
        _title = text;
        return this;
    }
}
